package team

import (
	"sanguo/cluster"
	"sanguo/node/node_team/com"
	cs_msg "sanguo/protocol/cs/message"
	ss_msg "sanguo/protocol/ss/message"

	"github.com/golang/protobuf/proto"
	"github.com/sniperHW/kendynet"
)

func onTeamList(session kendynet.StreamSession, msg proto.Message) {
	req := msg.(*ss_msg.TeamList)

	toC := &cs_msg.TeamListToC{
		TeamCount: proto.Int32(int32(TeamMgr.GetTeamCount())),
	}

	frontNo := int(req.GetFrontNo() - 1)
	rearNo := int(req.GetRearNo() - 1)

	for index := frontNo; index <= rearNo && index < TeamMgr.GetTeamCount(); index++ {
		t := TeamMgr.GetTeamByIdx(index)
		if t != nil {
			_team := &cs_msg.Team{
				TeamID:   proto.Uint64(t.TeamID),
				TeamName: proto.String(t.TeamName),
				TeamType: proto.Int32(t.TeamType),
				Header:   proto.Uint64(t.Header),
			}
			for _, r := range t.GetRoles() {
				_role := &cs_msg.ViewObj{
					RoleID:     proto.Uint64(r.RoleID),
					UserID:     proto.String(r.UserID),
					Level:      proto.Int32(r.Level),
					Name:       proto.String(r.Name),
					Portrait:   proto.Int32(r.Portrait),
					Outline:    proto.Int32(r.Outline),
					Face:       proto.Int32(r.Face),
					Hair:       proto.Int32(r.Hair),
					HairColor:  proto.Int32(r.HairColor),
					EyeColor:   proto.Int32(r.EyeColor),
					Cloth:      proto.Int32(r.Cloth),
					ClothColor: proto.Int32(r.ClothColor),
					Weapon1:    proto.Int32(r.Weapon1),
				}
				_team.Roles = append(_team.Roles, _role)
			}
			toC.Teams = append(toC.Teams, _team)
		}
	}

	com.SendToClient(cluster.PeerID(req.GetGame()), req.GetUserID(), toC)

}

func init() {
	cluster.Register(&ss_msg.TeamList{}, func(session kendynet.StreamSession, msg proto.Message) {
		onTeamList(session, msg)
	})

}
